package com.ruitu.mall_98_seller.mvp.presenter;

import android.text.TextUtils;

import com.ruitu.arad.api.BaseResult;
import com.ruitu.arad.api.PageModel;
import com.ruitu.arad.base.BasePresenter;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.mall_98_seller.dao.bean.Book;
import com.ruitu.mall_98_seller.dao.bean.OrderFilter;
import com.ruitu.mall_98_seller.mvp.model.BookModel;
import com.ruitu.mall_98_seller.ui.book.BookApproveActivity;
import com.ruitu.mall_98_seller.ui.book.BookDetailActivity;
import com.ruitu.mall_98_seller.ui.book.BookListActivity;

import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class BookPresenter {

    // 预订单列表
    public static class BookListP extends BasePresenter<BookListActivity, BookModel.BookListM> {
        public Disposable reqBookList(String comId, int size, final int page, String status) {
            return m.reqBookList(comId, size, page, status).subscribe(new Consumer<BaseResult<PageModel<Book>>>() {
                @Override
                public void accept(BaseResult<PageModel<Book>> pageResult) throws Exception {
                    v.hideProgress();
                    if (pageResult.isSuccess()) {
                        v.onReqComplete(1, true, pageResult.getData());
                    } else {
                        ToastUtils.showShortSafe(pageResult.getMsg());
                    }
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    v.hideProgress();
                    throwable.printStackTrace();
                    ToastUtils.showShortSafe("请求异常,请稍候重试");
                }
            });
        }

        // 状态（0 未处理 1 多次修改 2 同意 3 拒绝）
        public void genOrderFilter(List<OrderFilter> filterList1) {
            // 状态筛选的数据
            filterList1.add(new OrderFilter("全部状态", ""));
            filterList1.add(new OrderFilter("待审核", "0"));
            filterList1.add(new OrderFilter("多次修改", "1"));
            filterList1.add(new OrderFilter("已同意", "2"));
            filterList1.add(new OrderFilter("已拒绝", "3"));
        }

        // 根据状态码获取状态文字
        public String getStatus(String s) {
            return TextUtils.equals("0", s) ? "待审核" : (TextUtils.equals("1", s) ? "多次修改" :
                    (TextUtils.equals("2", s) ? "已同意" : (TextUtils.equals("3", s) ? "已拒绝" : "未知状态")));
        }
    }

    // 预订单详情
    public static class BookDetailP extends BasePresenter<BookDetailActivity, BookModel.BookDetailM> {
        public Disposable reqBookDetail(String bookId) {
            return m.reqBookDetail(bookId).subscribe(new Consumer<BaseResult<Book>>() {
                @Override
                public void accept(BaseResult<Book> result) throws Exception {
                    v.hideProgress();
                    if (result.isSuccess()) {
                        v.onReqComplete(1, true, result.getData());
                    } else {
                        ToastUtils.showShortSafe(result.getMsg());
                    }
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    v.hideProgress();
                    throwable.printStackTrace();
                    ToastUtils.showShortSafe("获取预订信息异常,请稍后重试");
                }
            });
        }
    }

    // 预订单审核
    public static class BookApproveP extends BasePresenter<BookApproveActivity, BookModel.BookApproveM> {
        public Disposable reqBookDetail(String bookId) {
            return new BookModel.BookDetailM().reqBookDetail(bookId).subscribe(new Consumer<BaseResult<Book>>() {
                @Override
                public void accept(BaseResult<Book> result) throws Exception {
                    v.hideProgress();
                    if (result.isSuccess()) {
                        v.onReqComplete(1, true, result.getData());
                    } else {
                        ToastUtils.showShortSafe(result.getMsg());
                    }
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    v.hideProgress();
                    throwable.printStackTrace();
                    ToastUtils.showShortSafe("获取预订单信息异常,请稍后重试");
                }
            });
        }

        public Disposable reqDenyBook(String id) {
            return m.reqDenyBook(id).subscribe(new Consumer<BaseResult>() {
                @Override
                public void accept(BaseResult result) throws Exception {
                    if (result.isSuccess()) {
                        v.onReqComplete(2, true, result.getMsg());
                    } else {
                        ToastUtils.showShortSafe(result.getMsg());
                    }
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    v.hideProgress();
                    throwable.printStackTrace();
                    ToastUtils.showShortSafe("审核异常,请稍候重试");
                }
            });
        }

        public Disposable reqAgreeBook(String id, String preRatio, String sendTime, String proPrice) {
            return m.reqAgreeBook(id, preRatio, sendTime, proPrice)
                    .subscribe(new Consumer<BaseResult>() {
                        @Override
                        public void accept(BaseResult result) throws Exception {
                            v.hideProgress();
                            if (result.isSuccess()) {
                                v.onReqComplete(3, true, result.getMsg());
                            } else {
                                ToastUtils.showShortSafe(result.getMsg());
                            }
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            v.hideProgress();
                            throwable.printStackTrace();
                            ToastUtils.showShortSafe("审批异常,请稍候重试");
                        }
                    });
        }
    }
}
